package com.jilian.mydemo.modul.exam.ui;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jilian.mydemo.MyApplication;
import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseActivity;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.common.dto.WrongAnwserDto;
import com.jilian.mydemo.listener.CustomItemClickListener;
import com.jilian.mydemo.modul.exam.adapter.CheckWrongTopicAdapter;
import com.jilian.mydemo.modul.user.viewmodel.UserViewModel;
import com.jilian.mydemo.utils.EmptyUtils;
import com.jilian.mydemo.utils.ToastUitl;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 查看错题
 */
public class CheckWrongTopicActivity extends BaseActivity implements CustomItemClickListener {
    private RecyclerView recyclerview;
    private LinearLayoutManager linearLayoutManager;
    private List<WrongAnwserDto> datas;
    private CheckWrongTopicAdapter adapter;
    private UserViewModel viewModel;
    private SmartRefreshLayout srHasData;
    private SmartRefreshLayout srNoData;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.removeActivity(this);
    }



    @Override
    protected void createViewModel() {
        viewModel = ViewModelProviders.of(this).get(UserViewModel.class);
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_checkwrongtopic;
    }

    @Override
    public void initView() {
        setNormalTitle("查看错题", v -> finish());
        srHasData = (SmartRefreshLayout) findViewById(R.id.sr_has_data);
        recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
        srNoData = (SmartRefreshLayout) findViewById(R.id.sr_no_data);
        linearLayoutManager = new LinearLayoutManager(this);
        recyclerview.setLayoutManager(linearLayoutManager);
        datas = new ArrayList<>();
        adapter = new CheckWrongTopicAdapter(this,datas,this);
        recyclerview.setAdapter(adapter);
        srNoData.setEnableLoadMore(false);
        srHasData.setEnableLoadMore(false);

    }

    @Override
    public void initData() {
        showLoadingDialog();
        getErrorData();
    }

    /**
     * 查看错题
     */
    private void getErrorData() {

        viewModel.wrong();
        viewModel.getWrongliveData().observe(this, new Observer<BaseDto<List<WrongAnwserDto>>>() {
            @Override
            public void onChanged(@Nullable BaseDto<List<WrongAnwserDto>> dto) {
                hideLoadingDialog();
                srHasData.finishRefresh();
                srNoData.finishRefresh();
                if(dto.isSuccess()){
                     if(EmptyUtils.isNotEmpty(dto.getData())){
                         datas.clear();
                         datas.addAll(dto.getData());
                         adapter.notifyDataSetChanged();
                     }

                }
                else{
                    ToastUitl.showImageToastTips(dto.getMsg());
                }
            }
        });
    }

    @Override
    public void initListener() {
        srNoData.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                getErrorData();
            }
        });
        srHasData.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getErrorData();
            }
        });
    }

    @Override
    public void onItemClick(View view, int position) {

    }
}
